package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ColumnInformation;
import com.innovations.inn.service.IColumnInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 栏目信息Controller
 *
 * @author dp
 * @date 2024-08-10
 */
@Api(tags = "栏目信息")
@RestController
@RequestMapping("/inn/information")
public class ColumnInformationController extends BaseController {
    @Autowired
    private IColumnInformationService columnInformationService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询栏目信息列表
     */
    @ApiOperation("查询栏目信息列表")
    @PreAuthorize("@ss.hasPermi('inn:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(ColumnInformation columnInformation) {
        startPage();
        List<ColumnInformation> list = columnInformationService.selectColumnInformationList(columnInformation);
        return getDataTable(list);
    }

    /**
     * 导出栏目信息列表
     */
    @PreAuthorize("@ss.hasPermi('inn:information:export')")
    @Log(title = "栏目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ColumnInformation columnInformation) {
        List<ColumnInformation> list = columnInformationService.selectColumnInformationList(columnInformation);
        ExcelUtil<ColumnInformation> util = new ExcelUtil<ColumnInformation>(ColumnInformation.class);
        util.exportExcel(response, list, "栏目信息数据");
    }

    /**
     * 获取栏目信息详细信息
     */
    @ApiOperation("获取栏目信息详细信息")
    @PreAuthorize("@ss.hasPermi('inn:information:query')")
    @GetMapping(value = "/{columnInformationId}")
    public AjaxResult getInfo(@PathVariable("columnInformationId") Long columnInformationId) {
        return success(columnInformationService.selectColumnInformationByColumnInformationId(columnInformationId));
    }

    /**
     * 新增栏目信息
     */
    @ApiOperation("新增栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:add')")
    @Log(title = "栏目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ColumnInformation columnInformation) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        columnInformationService.completeData(columnInformation, userId, 1);
        return toAjax(columnInformationService.insertColumnInformation(columnInformation));
    }

    /**
     * 修改栏目信息
     */
    @ApiOperation("修改栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:edit')")
    @Log(title = "栏目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ColumnInformation columnInformation) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        columnInformationService.completeData(columnInformation, userId, 2);
        return toAjax(columnInformationService.updateColumnInformation(columnInformation));
    }

    /**
     * 删除栏目信息
     */
    @ApiOperation("删除栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:remove')")
    @Log(title = "栏目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{columnInformationIds}")
    public AjaxResult remove(@PathVariable Long[] columnInformationIds) {
        return toAjax(columnInformationService.deleteColumnInformationByColumnInformationIds(columnInformationIds));
    }
}
